package com.platform.modules.wallet.controller;

import cn.hutool.core.util.StrUtil;
import com.platform.common.shiro.ShiroUtils;
import com.platform.common.shiro.utils.Md5Utils;
import com.platform.common.version.ApiVersion;
import com.platform.common.version.VersionEnum;
import com.platform.common.web.domain.AjaxResult;
import com.platform.modules.chat.domain.ChatUser;
import com.platform.modules.chat.service.ChatUserService;
import com.platform.modules.sms.service.SmsService;
import com.platform.modules.wallet.domain.Wallet;
import com.platform.modules.wallet.enums.IdentityStatusEnum;
import com.platform.modules.wallet.service.WalletService;
import com.platform.modules.wallet.utils.BankCardUtil;
import com.platform.modules.wallet.utils.VerifyFaceUtil;
import com.platform.modules.wallet.utils.VerifyIdcardUtil;
import com.platform.modules.wallet.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 钱包-账户
 */
@RestController
@Slf4j
@RequestMapping("/wallet")
public class AccountController {

    @Resource
    private WalletService walletService;
    @Resource
    private SmsService smsService;
    @Resource
    private ChatUserService chatUserService;

    /**
     * 是否实名认证
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @GetMapping("/getIdentityStatus")
    public AjaxResult getIdentityStatus() {
        Long userId = ShiroUtils.getUserId();
        return AjaxResult.success(walletService.getWallet(userId).getIdentityStatus());
    }

    /**
     * 完善用户信息
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @PostMapping("/perfectUserInfo")
    public AjaxResult perfectUserInfo(@Validated @RequestBody WalletVo02 vo){
        Long userId = ShiroUtils.getUserId();
        Wallet wallet = walletService.getWallet(userId);

        if (IdentityStatusEnum.YES.equals(wallet.getIdentityStatus())){
            return AjaxResult.fail("修改失败，已经认证过了");
        }
        if (IdentityStatusEnum.DO_ING.equals(wallet.getIdentityStatus())){
            return AjaxResult.fail("身份认证中");
        }

        boolean verify = VerifyIdcardUtil.verify(vo.getName(), vo.getIdentityCard());

        wallet.setName(vo.getName());
        wallet.setIdentityCard(vo.getIdentityCard());
        if (verify){
            wallet.setIdentityStatus(IdentityStatusEnum.DO_ING);
        }else {
            wallet.setIdentityStatus(IdentityStatusEnum.FAIL);
        }
        wallet.setPhotoUrl(vo.getPhotoUrl1()+","+vo.getPhotoUrl2());

        walletService.updateById(wallet);

        return AjaxResult.success(1,"修改成功");
    }

    /**
     * 人脸识别认证
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @PostMapping("/faceVerity")
    public AjaxResult faceVerity(@Validated @RequestBody WalletVo03 vo){
        Long userId = ShiroUtils.getUserId();
        Wallet wallet = walletService.getWallet(userId);
        try {
            boolean verify = VerifyFaceUtil.verify(wallet.getName(), wallet.getIdentityCard(), vo.getFaceUrl());
            if (verify){
                wallet.setIdentityStatus(IdentityStatusEnum.YES);
                walletService.updateById(wallet);
                return AjaxResult.success(1,"认证成功");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return AjaxResult.fail("人脸认证失败");
    }

    /**
     * 是否绑定银行卡
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @GetMapping("/getBankBind")
    public AjaxResult getBankBind() {

        Long userId = ShiroUtils.getUserId();
        Wallet wallet = walletService.getWallet(userId);

        if (StrUtil.isBlank(wallet.getCardNumber())
                || StrUtil.isBlank(wallet.getBankName())){
            return AjaxResult.success(0,"银行卡未绑定");
        }
        return AjaxResult.success(1,"银行已绑定");
    }

    /**
     * 查询银行卡信息
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @GetMapping("/getCardInfo/{cardNo}")
    public AjaxResult getCardInfo(@PathVariable String cardNo) {
        return AjaxResult.success(BankCardUtil.getBankInfo(cardNo));
    }

    /**
     * 设置银行卡信息
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @PostMapping("/setBankInfo")
    public AjaxResult setBankInfo(@Validated @RequestBody BankCardVo vo) {

        Long userId = ShiroUtils.getUserId();
        Wallet wallet = walletService.getWallet(userId);
        wallet.setBankName(vo.getBankName());
        wallet.setCardNumber(vo.getCardNumber());

        walletService.updateById(wallet);

        return AjaxResult.success("银行卡信息设置成功");
    }

    /**
     * 是否已设置支付密码
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @GetMapping("/hasPayPassword")
    public AjaxResult hasPayPassword() {
        Long userId = ShiroUtils.getUserId();
        Wallet wallet = walletService.getWallet(userId);
        if (StrUtil.isBlank(wallet.getPayPassword())){
            return AjaxResult.success(0,"支付密码未设置");
        }
        return AjaxResult.success(1,"支付密码已设置");
    }

    /**
     * 获取手机号
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @GetMapping("/getPhone")
    public AjaxResult getPhone() {

        // 当前用户
        ChatUser cu = chatUserService.getById(ShiroUtils.getUserId());

        return AjaxResult.success(cu.getPhone());
    }

    /**
     * 设置支付密码
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @PostMapping("/setPayPassword")
    public AjaxResult setPayPassword(@Validated @RequestBody PasswordVo02 passwordVo) {

        Long userId = ShiroUtils.getUserId();
        Wallet wallet = walletService.getWallet(userId);

        if (!passwordVo.getPayPassword().equals(passwordVo.getRePayRasswowd())){
            return AjaxResult.fail("两次密码输入不一致，请重试");
        }

        // 当前用户
        ChatUser cu = chatUserService.getById(ShiroUtils.getUserId());
        // 验证
//        smsService.verifySms(cu.getPhone(), passwordVo.getCode(), SmsTypeEnum.PASSWORD);
        String payPassword = Md5Utils.credentials(passwordVo.getPayPassword(), cu.getSalt());
        wallet.setPayPassword(payPassword);

        walletService.updateById(wallet);

        return AjaxResult.success("支付密码设置成功");
    }

    /**
     * 修改支付密码
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @PostMapping("/updatePayPassword")
    public AjaxResult updatePayPassword(@Validated @RequestBody PasswordVo01 passwordVo) {

        Long userId = ShiroUtils.getUserId();
        Wallet wallet = walletService.getWallet(userId);

        // 当前用户
        ChatUser cu = chatUserService.getById(ShiroUtils.getUserId());
        String payPassword = Md5Utils.credentials(passwordVo.getPayPassword(), cu.getSalt());

        if (!payPassword.equals(wallet.getPayPassword())){
            return AjaxResult.fail("旧支付密码验证失败");
        }

        String newPayPassword = Md5Utils.credentials(passwordVo.getNewPayRasswowd(), cu.getSalt());

        wallet.setPayPassword(newPayPassword);

        walletService.updateById(wallet);

        return AjaxResult.success("支付密码设置成功");
    }

    /**
     * 校验支付密码
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @PostMapping("/verifyPayPassword")
    public AjaxResult verifyPayPassword(@Validated @RequestBody PasswordVo vo) {

        Long userId = ShiroUtils.getUserId();
        Wallet wallet = walletService.getWallet(userId);

        // 当前用户
        ChatUser cu = chatUserService.getById(ShiroUtils.getUserId());
        String payPassword = Md5Utils.credentials(vo.getPayPassword(), cu.getSalt());

        if (StrUtil.equals(wallet.getPayPassword(),payPassword)){
            return AjaxResult.success("验证成功");
        }else {
            return AjaxResult.fail("密码有误");
        }
    }
}
